<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加分类</title>
    <meta name="keywords" content="" />
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/app.css">
    <script src="./js/config.js"></script>
    <script src="./js/jquery.min.js"></script>
</head>

<body>
    <nav id="navbar" class="navbar has-shadow">
        <div class="container">
            <div class="navbar-brand">
                <div class="navbar-burger burger" data-target="navMenu">
                    <span></span>
                    <span></span>
                    <span></span>
                </div>
            </div>
            <div id="navMenu" class="navbar-menu">
                <div class="navbar-start">
                    <a class="navbar-item" href="index.html">主页</a>
                    <a class="navbar-item" href="about.html">关于</a>
                </div>
                <div class="navbar-end">
                    <div class="navbar-item">
                        <a class="button is-small is-primary" href="category.html">分类</a>
                    </div>
                    <div class="navbar-item">
                        <a class="button is-small is-primary" href="add-article.html">写文章</a>
                    </div>
                    <div class="navbar-item">
                        <a href="user-articlelist.html">张三</a>
                    </div>
                    <div class="navbar-item">
                        <a href="javascript:void(0);">退出</a>
                    </div>
                    <div class="navbar-item">
                        <a class="button is-small is-primary" href="register.html">注册</a>
                    </div>
                    <div class="navbar-item">
                        <a class="button is-small" href="login.html">登录</a>
                    </div>
                </div>
            </div>
        </div>
    </nav>

    <div class="container margin-top">
        <div class="columns">
            <div class="column is-8 is-offset-2">
                <div>
                    <div class="field">
                        <label class="label">分类名</label>
                        <div class="control">
                            <input id="name" name="title" class="input" type="text" placeholder="微服务">
                        </div>
                    </div>
                    <div class="field">
                        <label class="label">描述</label>
                        <div class="control">
                            <textarea id="desc" name="desc" class="textarea" placeholder="描述信息"></textarea>
                        </div>
                    </div>
                    <button class="button is-primary" onclick="AddEvent()">新建分类</button>
                </div>
            </div>
        </div>
    </div>
</body>

<script>
    function AddEvent() {
        // 1、参数的验证
        var name = jQuery("#name");
        if (name.val().trim() == "") {
            alert("请输入要添加的分类！");
            name.focus();
            return;
        }
        jQuery.ajax({
            url: base_url + "/category/add",
            method: "POST",
            data: {
                name: name.val(),
                desc: jQuery("#desc").val()
            },
            // 携带 token 进行是否登录判断
            headers: {
                "Authorization": window.localStorage.getItem(token_key)
            },
            success: function (result) {
                if (result != null && result.code == 200 && result.data == true) {
                    alert("添加分类成功！");
                    if (confirm("是否还需要继续添加分类？")) {
                        name.val('');
                        jQuery("#desc").val('');
                        return;
                    } else {
                        location.href = "./category.html";
                    }
                } else {
                    alert("添加分类失败！");
                    return;
                }
            }
        });
    }

</script>

</html>